﻿using System;
using System.Windows.Forms;
using SoftwareMonkeys.LogClimber.Entities;
using System.Collections.Generic;

namespace SoftwareMonkeys.LogClimber.Controls
{
	/// <summary>
	/// Description of ComponentListBuilder.
	/// </summary>
	public class ComponentListBuilder
	{
		CheckedListBox componentList = null;
		
		public ComponentListBuilder(CheckedListBox componentList)
		{
			this.componentList = componentList;
		}
		
		
		public void Show(Log[] logs)
		{
			List<string> componentNames = new List<string>();
			foreach (Log log in logs)
				componentNames.AddRange(ComponentsExtractor.GetComponentNames(log));
			
			Show(componentNames.ToArray());
		}
		
		public void Show(Log log)
		{
			string[] componentNames = ComponentsExtractor.GetComponentNames(log);
			
			Show(componentNames);
		}
		
		public void Show(string[] componentNames)
		{
			string[] selectedComponentNames = GetSelectedComponents();
			
			componentList.Items.Clear();
			
			Array.Sort(componentNames);
			
			foreach (string componentName in componentNames)
			{
				bool isChecked = true;
				
				if (selectedComponentNames.Length > 0)
					isChecked = IsSelected(componentName, selectedComponentNames);
				
				componentList.Items.Add(componentName, isChecked);
				
			}
		}
		
		public bool IsSelected(string componentName, string[] selectedComponentNames)
		{
			bool isSelected = Array.IndexOf(selectedComponentNames, componentName) > -1;
			return isSelected;
		}
		
		public string[] GetSelectedComponents()
		{
			List<string> list = new List<string>();
			for (int i = 0; i < componentList.CheckedItems.Count; i++)
			{
				list.Add((string)componentList.CheckedItems[i]);
			}
			
			return list.ToArray();
		}
		
		/*public void SetSelectedComponents()
		{
			
			List<string> list = new List<string>();
			for (int i = 0; i < componentList.Items.Count; i++)
			{
				componentList.Items[i].C
			}
			
			return list.ToArray();
		}*/
		
	}
}
